<?php
/**
 * @version 	$Id: fUri.class.php 74 2010-03-17 06:48:21Z mylifeisskidrow@gmail.com $
 * @author 		$Author: mylifeisskidrow@gmail.com $
 * @copyright 	Copyright (c) 2010 Luu Trong Hieu. All right reserved.
 * @license 	GNU/GPL see license.php
 */

defined('IN_FLYWHEEL') or die('Restricted Access!');

/**
 * URI
 * 
 * @package 	system
 * @subpackage 	environment
 *
 */

class fUri {
	public function __construct() {}	
	
	/**
	 * Create Url
	 *
	 * @param string $component	Component Name
	 * @param string $action	Action Name
	 * @param array $param		
	 * @param boolean $entire
	 * @return string
	 */
	public static function createUrl($component, $action = null, $param = array(), $entire = true) {
		/**
		 * @FIXME: chua tao URL voi culture info
		 */
		$url = '';
		$config = fConfig::getConfigByNamespace('routing');		
		if ($config['friendly_url'] == 1) {			
			$url .= $config['url_structure'];
			$url = str_replace('<component>', $component, $url);						
			$url = str_replace('<action>', $action, $url);		
			//build param
			if (strpos($url, '*') !== false) {				
				if (sizeof($param) > 0 && is_array($param)) { // Build param
					$paramString = '';
					foreach ($param as $key=>$value) {
						$paramString.= '/'.$key.':'.$value;											 			
					}
					$url = str_replace('*', $paramString, $url);
				}				
			}
			$url = str_replace('*', '', $url);		
			$url = preg_replace('/(\/\/+)/', '/', $url);			
			$url = rtrim($url, '/');
			if (isset($config['url_suffix'])) { // attach suffix				 				
				$url .= $config['url_suffix'];		 
			}
			if ($entire) $url = rtrim(fFactory::getRouter()->getBaseUrl(), '/') . '/' . $url;		
		}
		else {
			$url .='?com='.$component .(($action != null)? '&act='.$action:'');						
			if (sizeof($param) > 0 && is_array($param)) {
				foreach ($param as $key=>$value) {
					$url.='&'.$key.'='.$value;											 			
				}
			}
			if ($entire) $url = rtrim(fFactory::getRouter()->getBaseUrl(), '/') . $url;
		}
		return $url;
	}
}